package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.yljf.domain.BookCategory;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.BookVO;
import com.ruoyi.yljf.service.IBookCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.BookMapper;
import com.ruoyi.yljf.domain.Book;
import com.ruoyi.yljf.service.IBookService;

import javax.annotation.Resource;

/**
 * 书籍Service业务层处理
 *
 * @author metaee
 * @date 2025-01-03
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Resource
    private BookMapper bookMapper;
    @Autowired
    private IBookCategoryService categoryService;

    /**
     * 查询书籍列表
     *
     * @param book 书籍
     * @return 书籍
     */
    @Override
    public List<Book> selectBookList(Book book) {
        LambdaQueryWrapper<Book> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(book.getName())) {
            queryWrapper.like(Book::getName, book.getName());
        }
        if (StringUtils.isNotEmpty(book.getAuthor())) {
            queryWrapper.like(Book::getAuthor, book.getAuthor());
        }
        if (StringUtils.isNotNull(book.getCategoryId())) {
            queryWrapper.eq(Book::getCategoryId, book.getCategoryId());
        }
        if (StringUtils.isNotEmpty(book.getFree())) {
            queryWrapper.eq(Book::getFree, book.getFree());
        }
        return bookMapper.selectList(queryWrapper);

    }

    @Override
    public List<BookVO> qyeryBookList(Book book) {

        List<Book> books = this.selectBookList(book);

        if (CollectionUtils.isEmpty(books)){
            return Collections.emptyList();
        }

        List<Long> categoryIds = books.stream().map(Book::getCategoryId).collect(Collectors.toList());
        List<BookCategory> bookCategories = categoryService.listByIds(categoryIds);
        List<BookVO> vos = new ArrayList<>();
        for (Book book1 : books) {
            BookVO bookVO = new BookVO(book1);
            for (BookCategory bookCategory : bookCategories) {
                if (bookCategory.getId().equals(book1.getCategoryId())) {
                    bookVO.setCategoryName(bookCategory.getName());
                    break;
                }
            }
            vos.add(bookVO);
        }

        return vos;

    }

    @Override
    public BookVO queryById(Long id) {
        Book book = this.getById(id);
        BookVO bookVO = new BookVO(book);
        BeanUtils.copyProperties(book, this);
        bookVO.setFree(yljfEnums.descOfCode(book.getFree()));
        BookCategory category = categoryService.getById(book.getCategoryId());
        bookVO.setCategoryName(category.getName());
        return bookVO;
    }


}
